//数值转换

const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const types = require("@babel/types");
const generator = require("@babel/generator").default;

const jscode = `var a = window;let b = a.btoa("Hello,AST");`; //转换为i var b=3;

const ast = parser.parse(jscode);
const visitor = {
    VariableDeclarator(path) {
        const {id ,init}=path.node;
        if (!types.isIdentifier(init,{name:"window"})){return ;}
        let name=id.name;
        let scope=path.scope;
        const binding=scope.getBinding(name);// 获取windows在当前节点的作用域中的值
        if (!binding || binding.constantViolations.length!==0){return;}
        scope.rename(name,"window")   //rename 修改所有绑定此变量的值
        path.remove() //删除当前节点
    }
}

traverse(ast, visitor);
console.log(generator(ast)['code'])




